﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QuanLyKho.BL;
using QuanLyKho.Entity;
using DevExpress.XtraEditors;

namespace QuanLyKho.GUI
{
    public partial class uXuatKho : DevExpress.XtraEditors.XtraUserControl
    {
        DataTable dt_NhapKho = new DataTable();

        DataTable dtHangHoa = new DataTable();
        public uXuatKho()
        {
            InitializeComponent();
            dt_NhapKho.Columns.Add("maHang", typeof(String));
            dt_NhapKho.Columns.Add("tenHang", typeof(String));
            dt_NhapKho.Columns.Add("donGia", typeof(String));
            dt_NhapKho.Columns.Add("soLuong", typeof(String));
            dt_NhapKho.Columns.Add("thanhTien", typeof(String));
            gridControl1.DataSource = dt_NhapKho;
            bHangHoa bh = new bHangHoa();
            dtHangHoa = bh.getAllHangHoa();
            ItemSearchLookUpMaHang.DataSource = dtHangHoa;
            dt_NhapKho.ColumnChanging += new DataColumnChangeEventHandler(dt_NhapKho_ColumnChanging);
        }
        void dt_NhapKho_ColumnChanging(object sender, DataColumnChangeEventArgs e)
        {
            // MessageBox.Show(e.Column.ColumnName);
            if (e.Column.ColumnName == "donGia" && (e.Row["soLuong"].ToString() != ""))
            {
                //MessageBox.Show("he");
                try
                {
                    int soluong1 = int.Parse(e.Row["soLuong"].ToString());
                    float dongia1 = float.Parse(e.ProposedValue.ToString());
                    float thanhTien = soluong1 * dongia1;
                    e.Row["thanhTien"] = thanhTien.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                }
            }
            else if (e.Column.ColumnName == "soLuong" && (e.Row["donGia"].ToString() != ""))
            {
                //MessageBox.Show("he");
                try
                {
                    int soluong1 = int.Parse(e.ProposedValue.ToString());
                    float dongia1 = float.Parse(e.Row["donGia"].ToString());
                    float thanhTien = soluong1 * dongia1;
                    e.Row["thanhTien"] = thanhTien.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                }

            }
            else if (e.Column.ColumnName == "maHang")
            {
                //e.Row["tenHang"] = e.ProposedValue.ToString();
                DataRowView dv = timHangHoa(e.ProposedValue.ToString());
                e.Row["tenHang"] = dv["tenHang"];
            }

        }

        public DataRowView timHangHoa(String maHang)
        {
            DataView dv = dtHangHoa.DefaultView;
            // dv.RowFilter = "maHang='"+maHang+"'";
            dv.Sort = "maHang";
            return dv.FindRows(maHang)[0];
        }
        private void uXuatKho_Load(object sender, EventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            bXuatKHo bxk = new bXuatKHo();
            bXuatKhoDetail bxkd = new bXuatKhoDetail();

            eXuatKho exk= new eXuatKho();
            exk.ngayXuat = txtNgayNhap.Value;
            exk.Ten = txtTen.Text;
            exk.diaChi = txtDiaChi.Text;
            exk.soDt = txtSoDt.Text;
            exk.GhiChu = txtGhiChu.Text;

            int xkId = bxk.insertXuatKho(exk);
            for (int i = 0; i < dt_NhapKho.Rows.Count; i++)
            {
                DataRow dr = (dt_NhapKho.Rows[i]);
                eXuatKhoDetail enkd = new eXuatKhoDetail();
                enkd.maHang = dr["maHang"].ToString();
                enkd.nkId = xkId;
                enkd.donGia = float.Parse(dr["donGia"].ToString());
                enkd.soLuong = int.Parse(dr["soLuong"].ToString());
                bxkd.insertXuatKhoDetail(enkd);
            }
            MessageBox.Show("Đã xuất kho thành công!");
        }
    }
}
